/* 
 * This code is part of the smartlink project, see
 * 
 * 		http://code.google.com/p/smartlink/.
 * 
 * Copyright (C) 2008 Oliver Noelle
 * 
 * SmartLink is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * SmartLink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SmartLink.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.smartlink;

import org.smartlink.provider.SmartLink;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * An activity that will edit the key of a link. Displays a floating window with
 * a text field.
 * 
 * Note: Does not persist the key's text when activity resumed...
 */
public class KeyEditor extends Activity implements View.OnClickListener {
	/**
	 * This is a special intent action that means "edit the title of a note".
	 */
	public static final String EDIT_KEY_ACTION = "org.smartlink.action.EDIT_KEY";

	/**
	 * The EditText field from our UI. Keep track of this so we can extract the
	 * text when we are finished.
	 */
	EditText mText;

	/** The different distinct states the activity can be run in. */
	private int mState;

	/** editing a already existing link */
	private static final int STATE_EDIT = 0;
	/** editing a to-be-inserted new link */
	private static final int STATE_INSERT = 1;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.key_editor);

		String action = getIntent().getAction();
		if (action.equals(Intent.INSERT_ACTION)) {
			mState = STATE_INSERT;

		} else if (action.equals(Intent.EDIT_ACTION)) {
			mState = STATE_EDIT;
		}
		String key = getIntent().getStringExtra(SmartLink.Links.EXTRA_CREATE_LINK);

		// Set up click handlers for the text field and button
		mText = (EditText) this.findViewById(R.id.key);
		mText.setOnClickListener(this);
		mText.setMaxLines(1);
		mText.setSingleLine();
		mText.setText(key);

		Button b = (Button) findViewById(R.id.ok);
		b.setOnClickListener(this);
		Button c = (Button) findViewById(R.id.cancel);
		c.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mState == STATE_EDIT) {
			setTitle(getText(R.string.title_edit));
		} else {
			setTitle(getText(R.string.title_edit_key));
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	public void onClick(View v) {
		if (v.getId() == R.id.cancel) {
			setResult(RESULT_CANCELED);
			finish();
			return;
		} else {
			setResult(RESULT_OK, mText.getText().toString());
			finish();
		}
	}
}
